/**
 * @ClassName: UserOrderUtils
 * @author linsen
 * @date 2016-10-23
 */
package com.fzrskj.cloud.free.user.utils;

import com.fzrskj.cloud.free.core.enums.ProductTypeEnum;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.user.bean.UserCart;
import com.fzrskj.cloud.free.user.bean.statistics.Statistics;
import com.fzrskj.cloud.free.user.bean.UserOrder;
import com.fzrskj.cloud.free.user.bean.UserOrderView;
import com.fzrskj.cloud.free.user.dao.UserOrderDao;
import com.winiis.component.common.utils.tool.CSysTool;
import ms.core.object.CObject;
import ms.core.tool.StrTool;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.*;

public class UserOrderUtils extends CObject {
	/**
	 * 查询到期未支付订单
	 * @param dateTime
	 * @return
	 */
	public List<UserOrder> queryExpireNoPayOrder(String dateTime){
		UserOrderDao dao=new UserOrderDao();
		List<UserOrder> rets = dao.queryExpireNoPayOrder(dateTime);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return rets;
	}

	/**
	 * 获取订单详情
	 * @param orderId
	 * @return
	 */
	public UserOrder getUserOrder(String orderId){
		UserOrderDao dao=new UserOrderDao();
		UserOrder list = dao.getUserOrder(orderId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	public UserOrder getUserOrderWithoutAgent(String orderId) {
		UserOrderDao dao=new UserOrderDao();
		UserOrder list = dao.getUserOrderWithoutAgent(orderId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 获取订单详情视图
	 * @param orderId
	 * @return
	 */
	public UserOrderView getUserOrderView(String orderId){
		UserOrderDao dao=new UserOrderDao();
		UserOrderView list = dao.getUserOrderView(orderId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 删除订单信息
	 * @param cdt
	 * @return
	 */
	public int delUserOrder(Map<String, Object> cdt){
		UserOrderDao dao=new UserOrderDao();
		int ret = dao.delUserOrder(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 统计订单数
	 * @param cdt
	 * @return
	 */
	public int queryUserOrderCount(Map<String, Object> cdt){
		UserOrderDao dao=new UserOrderDao();
		int ret = dao.queryUserOrderCount(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 查询订单列表视图
	 * @param cdt
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public List<UserOrderView> queryUserOrderViewList(Map<String, Object> cdt, int page, int pagesize){
		UserOrderDao dao=new UserOrderDao();
		List<UserOrderView> list = dao.queryUserOrderViewList(cdt, page, pagesize);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return list;
	}

	/**
	 * 修改订单
	 * @param values
	 * @param cdt
	 * @return
	 */
	public int modUserOrder(Map<String, Object> values, Map<String, Object> cdt){
		UserOrderDao dao=new UserOrderDao();
		int i = dao.modUserOrder(values, cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return i;
	}

	/**
	 * 修改订单
	 * @param userOrder
	 * @return
	 */
	public int modUserOrder(UserOrder userOrder){
		//修改内容
		Map<String, Object> values = new HashMap<String, Object>();
		if(!StrTool.isBlankStr(userOrder.getHandleStatus())) {
			values.put("handleStatus", userOrder.getHandleStatus());
		}
		if(!StrTool.isBlankStr(userOrder.getPayStatus())) {
			values.put("payStatus", userOrder.getPayStatus());
		}
		if(!StrTool.isBlankStr(userOrder.getServiceId())) {
			values.put("serviceId", userOrder.getServiceId());
		}
		if(!StrTool.isBlankStr(userOrder.getOrderParam())) {
			values.put("orderParam", userOrder.getOrderParam());
		}

		//条件
		Map<String, Object> cdt = new HashMap<String, Object>();
		if(!StrTool.isBlankStr(userOrder.getOrderId())){
			if(userOrder.getOrderId().trim().equals("")){
				return -1;
			}
			cdt.put("orderId", userOrder.getOrderId());
		}

		UserOrderDao dao=new UserOrderDao();
		int i = dao.modUserOrder(values, cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return i;
	}

	//添加订单
	public String addUserOrder(int userId,String cartNos){
		UserCartUtils utils=new UserCartUtils();
		List<UserCart> list=utils.getUserCartBuyList(userId, cartNos);
		if(list==null) {
			return "";
		}

		String orderIds="";
		UserOrderDao dao=new UserOrderDao();
		for(UserCart uc: list){

			String orderId="";
			UserOrder order=new UserOrder();
			orderId="G"+new SimpleDateFormat("yyyyMMdd").format(new Date())+ CSysTool.getRandomNum(100000, 999999);
			order.setOrderId(orderId);
			order.setUserId(uc.getUserId());
			order.setUserName(uc.getUserName());
			order.setProductClass(uc.getProductClass());
			order.setProductCode(uc.getProductCode());
			order.setOrderParam(uc.getProductParam());
			order.setApplyTime(uc.getApplyTime());
			order.setPriceType(uc.getPriceType());

			double price=uc.getPrice();
			order.setPayAmount(uc.getPrice());
			order.setTotalAmount(uc.getTotalPrice());
			order.setIntegral((int)(price/10));
			order.setPayStatus("N");
			order.setHandleStatus("N");
			order.setOrderType(uc.getCartType());
			order.setOrderDate(new Date());
			order.setRegType(uc.getRegType());
			order.setCartNo(uc.getCartNo());
			int ret=dao.addUserOrder(order);

			if(ret>0){
				if(CStrTool.isBlankStr(orderIds)) {
					orderIds = orderId;
				} else {
					orderIds = orderIds + "," + orderId;
				}
			}
		}
		dao.dispose();
		utils.delUserCart(userId, cartNos);
		this.setErrCode(utils.getErrCode());
		return orderIds;
	}

	//获取订单(指定订单号)
	public List<UserOrderView> getUserOrderList(int userId,String orderIds){
		List<String> orderIdList=new ArrayList<String>();
		String[] arr=orderIds.split(",");
		for(String str:arr){
			orderIdList.add(str);
		}

		UserOrderDao dao=new UserOrderDao();
		List<UserOrderView> list=dao.getUserOrderList(userId, orderIdList);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return list;
	}

	/**
	 * 统计符合条件的订单数
	 * @param cdt
	 * @return
	 */
	public int queryOrderExistCount(Map<String, Object> cdt){
		UserOrderDao dao=new UserOrderDao();
		int ret = dao.queryOrderExistCount(cdt);
		this.setErrCode(dao.getErrCode());
		dao.dispose();
		return ret;
	}

	/**
	 * 查询删除订单
	 * @param serviceId
	 * @return
	 */
	public UserOrder queryDelOrder(String serviceId){
		UserOrderDao dao=new UserOrderDao();
		UserOrder list = dao.queryDelOrder(serviceId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	/**
	 * 按时间段统计符合条件的订单数
	 * @param cdt
	 * @param qryType H 小时 D 天 M 月 Y 年
	 * @return
	 * add wzj by 2018/12/12
	 */
	public List<Statistics> queryOrderCount(Map<String, Object> cdt, String qryType){
		UserOrderDao dao = new UserOrderDao();
		List<Statistics> rets = dao.queryOrderCount(cdt, qryType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return rets;
	}

	/**
	 * 判断域名首年价格是否低于代理价格
	 * @param userOrder 订单信息
	 * @return
	 * add wzj by 2019/08/02/
	 */
	public Boolean checkIsPremiumDomain(UserOrder userOrder){
		Boolean flag = false;
		do {
			//数据校验
			if (userOrder ==  null){
				break;
			}
			if (!ProductTypeEnum.DOMAIN.getTag().equals(userOrder.getProductClass())){
				break;
			}

			if (Integer.valueOf(userOrder.getRegType()) == 6){
				//获取系统首年价格
				JSONObject domainparam = JSONObject.fromObject(userOrder.getOrderParam());
				if (!domainparam.containsKey("domainName")){
					break;
				}
				String domainName = domainparam.getString("domainName");
				int sysPrice = UserCartUtils.queryUserOrderDomainFirstYearPrice(userOrder);
				if (sysPrice < 0){
					break;
				}
			} else {
				break;
			}
		}while (false);

		return flag;
	}
}
